$(function () {
    $(".notice-panel .startTime").jeDate(initjeDate);
    $(".notice-panel .endTime").jeDate(initjeDate);
})

function closeNoticeWin() {
    $(".notice-win").fadeOut();
}

function operateBtnClick() {
    var $tr = $(this).parents("tr");
    var id = $tr.find("td:first").text();
    if ($(this).hasClass("opt-enable")) {
        var status = $(this).attr("data");
        optBtnStatusFn(id, status);
    } else if ($(this).hasClass("opt-del")) {
        var status = $(this).attr("data");
        layer.confirm('您确定要删除吗，删除后不可再恢复！', {
            btn: ['确定', '取消']
        }, function () {
            optBtnStatusFn(id, status);
        })
    } else if ($(this).hasClass("opt-edit")) {
        optBtnEditFn(id);
    } else if ($(this).hasClass("opt-detail")) {
        optBtnDetailFn($tr);
    }
}

function optBtnStatusFn(id, status) {
    $.ajax({
        url: "/notice/back/updateStatusById.zb",
        type: "POST",
        dataType: "JSON",
        data: {id: id, status: status}
    }).done(function (res) {
        if (res && res.success) {
            loadNoticeList(true);
            layer.msg(res.msg);
        } else {
            layer.msg("操作失败！", fn);
        }
    }).fail(function () {
        layer.msg("可能由于网络等原因操作失败！", fn)
    })
}

function optBtnEditFn(id) {
    window.open("/page/notice/back/editNotice.html?id=" + id + "&type=edit")
}

function optBtnDetailFn($tr) {
    $(".notice-win").fadeIn();
    $(".notice-win h4.title").text($tr.find("td.title").text())
    $(".notice-win .person span").eq(0).text($tr.find("td.username").text());
    $(".notice-win .time span").eq(1).text($tr.find("td.createTime").text())
    $(".notice-win .notice-content").html($tr.data("content"));
    $(".notice-win .notice-content").niceScroll({
        cursorcolor: "#d24441",
        cursoropacitymax: 1,
        touchbehavior: false,
        cursorwidth: "2px",
        cursorborder: "0",
        cursorborderradius: "3px",
        autohidemode: false
    });
}

function loadNoticeList(isLoadPagination) {
    var param = {};
    param.pageNum = global_Art_Var.pageNum;
    param.pageSize = global_Art_Var.pageSize;
    param.title = $("#title").val().trim();
    param.enable = $("#enable").attr("data");
    param.startTime = $("#startTime").val().trim();
    param.endTime = $("#endTime").val().trim();

    $.ajax({
        url: "/notice/getNoticeAll.zb",
        type: "POST",
        dataType: "JSON",
        data: param
    }).done(function (data) {
        var $tbody = $("#notice-table tbody");
        $tbody.empty();
        if (data && data.list && data.list.length > 0) {
            var list = data.list;
            for (var i = 0; i < list.length; i++) {
                var l = list[i];
                var tr = "<tr>" +
                    "  <td>" + l.id + "</td>" +
                    "  <td style='text-align: left' class='title'>" + l.title + "</td>" +
                    "  <td class='createTime'>" + l.create_time + "</td>" +
                    "  <td class='username'>" + l.username + "</td>" +
                    "  <td>" + fmtStatus(l.enable) + "</td>" +
                    "  <td>" + fmtEmpty(l.descr) + "</td>" +
                    "  <td class=\"table-operate\">" +
                    "   <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'>" +
                    "       <a class='btn btn-danger  dropdown-toggle' id='dropdownMenu1' data-toggle='dropdown'>" +
                    "             操作<i class='fa fa-sort-desc fa-lg'></i>" +
                    "       </a>" +
                    "       <ul class='dropdown-menu' role='menu' aria-labelledby='dropdownMenu1'>" +
                    "           <li role='presentation' >" +
                    "           <a class='opt-enable' data='1'>启用</a>" +
                    "           </li>" +
                    "           <li role='presentation'>" +
                    "           <a class='opt-enable' data='0'>禁用</a>" +
                    "           </li>" +
                    "           <li role='presentation'>" +
                    "           <a class='opt-del' data='-1' >删除</a>" +
                    "           </li>" +
                    "       </ul>" +
                    "   </span>" +
                    "   <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'><a class='btn btn-warning opt-edit'><i class='fa fa-edit fa-md'></i> 编辑 </a></span>" +
                    "   <span class='col-lg-4 col-md-4 col-sm-12 col-xs-12'><a class='btn btn-primary opt-detail'><i class='fa fa-navicon fa-md'></i> 查看 </a></span>"
                "  </td>"
                "</tr>";
                var $tr = $(tr).data("content", l.content);
                $tbody.append($tr)
            }
        }
        global_Art_Var.totalPage = data.pages;
        if (isLoadPagination || global_Art_Var.isLoadPagination) {
            global_Art_Var.isLoadPagination = false;
            $(".table-page ul.pagination li").not(".page-prev,.page-next").remove();
            var lis = "";
            for (var i = 0; i < data.pages; i++) {
                if (i == 0) {
                    lis += " <li class='active'><a href='javascript:void(0)'>" + (i + 1) + "</a></li>";
                } else {
                    lis += " <li><a href='javascript:void(0)'>" + (i + 1) + "</a></li>";
                }
            }
            if (global_Art_Var.totalPage === 1) {
                $(".table-page ul.pagination li.page-next").addClass("disabled");
                $(".table-page ul.pagination li.page-next").addClass("disabled");
            }
            $(".table-page ul.pagination li.page-prev").after(lis);
        }
    }).fail(function () {
        layer.msg('可能由于网络等原因加载失败！', function () {
        });
    }).always(function () {
        $(".content_loading").hide();
    });
}

function keyDownSearch(e) {
    var keyCode = e.keyCode;
    if (keyCode == 13) {
        global_Art_Var.pageNum = 1;//重置分页
        loadNoticeList(true);
        return false;
    }
}

function tableFilterResetClick() {
    var $input = $(".notice-panel .panel-body form input");
    $input.val("");
    global_Art_Var.pageNum = 1;//重置分页
    loadNoticeList(true);
}

function fmtStatus(value) {
    if (!value && value != 0) {
        return "";
    }
    if (value == 0) {
        return "禁用";
    } else if (value == 1) {
        return "启用";
    } else {
        return "";
    }
}